Adding the code for the CEmbeddable_Designer class

To return to the previous part of this procedure, go to Adding directives and constants to the source and header files.

Type or insert the code for the member functions created in Adding member variables and functions to the CEmbeddable_Designer class. The CEmbeddable_DesignerDlg class handles the basic functionality of the Embeddable Designer sample application. The class will handle all of the report functionality including creating a new report or opening an existing report, and displaying it in the Embeddable Designer or the Crystal Report Viewer. Once a report is designed or edited it can be saved, previewed, or refreshed.

Note:    Error handling is not included in the code samples.

  1. Add the code to the OnInitDialog function.


 // Hide the Crystal Reports Viewer control and show the 
    // Embeddable Designer oontrol.
    m_Viewer.ShowWindow(SW_HIDE);

m_Designer.ShowWindow(SW_SHOW); // Hide the Crystal Reports Viewer control and show the

// Embeddable Designer oontrol.

m_Viewer.ShowWindow(SW_HIDE);

m_Designer.ShowWindow(SW_SHOW);


  1. Add the code for the InitReport function.


// In this function a report is created or opened. The report is set to 
// the Embeddable Designer and Crystal Report Viewer, with the Embeddable 
// Designer visible and the Crystal Report Viewer hidden. Then the 
// command buttons are initialized so that Save Report, and Preview are 
// enabled, and Design is disabled and hidden. 
void CEmbeddable_DesignerDlg::InitReport(BOOL bNew)
{
    // Create a new report for designing and previewing.
    if(bNew)
    {
        m_Application.CreateInstance("CrystalDesignRuntime.Application");
        m_Report = m_Application->NewReport();
    }
    
    // Show an Open dialog box, and then browse and open a report
    // for editing and previewing.
    else
    }
        // Initialize the Open dialog box to only display the 
        // Crystal Report(.rpt) file type.
        CFileDialog FileDlg(TRUE,"*.rpt",NULL, OFN_EXPLORER, szFilter);

        // Show the Open dialog box
        int iReturn = FileDlg.DoModal();
        
        if(iReturn == IDOK)
        {
            // Get the name and path of the report file selected from the 
            // Open dialog box. 
            _bstr_t FileName(FileDlg.GetPathName().AllocSysString());
            
            // Create the Application Object.
            
m_Application.CreateInstance("CrystalDesignRuntime.Application");

            // Open the report selected from the Open dialog box.
            m_Report = m_Application->OpenReport(FileName);
        }
        else if (iReturn = IDCANCEL)
        {
            // Terminate the execution of the function if the
            // user cancels.
            return;
        }
    }
    
    // Set the report object to the Embeddable Designer. 
    m_Designer.SetReportObject(m_Report);

    // Set the report souce for the Crystal Reports Viewer
    m_Viewer.SetReportSource(m_Report);
    
    // Set the width of the report page to the width of the viewing area
    m_Viewer.Zoom(1);
    
    // View the report in the Crystal Reports Viewer control.
    m_Viewer.ViewReport();

    // Check if the Save Report button is enabled.
    // If the button is disabled then enable the Save Report, 
    // and Preview buttons. The Save Report button will always 
    // be enabled once the first report is opened or created.
    BOOL bEnabled;
    bEnabled = m_DesignerSettings.IsWindowEnabled();

    if( bEnabled == FALSE)
    {
        m_SaveReport.EnableWindow(TRUE);
        m_Preview.EnableWindow(TRUE);
    }

    // Check if the Embeddable Designer is hidden. 
    // If it is hidden then hide the Design button, show the 
    // Preview button, hide the Crystal Reports Viewer, and 
    // show the Embeddable Designer.
    if(!m_Designer.IsWindowVisible())
    {
        m_ShowDesigner.ShowWindow(SW_HIDE);
        m_Preview.ShowWindow(SW_SHOW);
        m_Viewer.ShowWindow(FALSE);
        m_Designer.ShowWindow(TRUE);
    }

}// In this function a report is created or opened. The report is set to

// the Embeddable Designer and Crystal Report Viewer, with the Embeddable

// Designer visible and the Crystal Report Viewer hidden. Then the

// command buttons are initialized so that Save Report, and Preview are

// enabled, and Design is disabled and hidden.

void CEmbeddable_DesignerDlg::InitReport(BOOL bNew)

{

    // Create a new report for designing and previewing.

    if(bNew)

    {

        m_Application.CreateInstance("CrystalDesignRuntime.Application");

        m_Report = m_Application->NewReport();

    }

    

    // Show an Open dialog box, and then browse and open a report

    // for editing and previewing.

    else

    }

        // Initialize the Open dialog box to only display the

        // Crystal Report(.rpt) file type.

        CFileDialog FileDlg(TRUE,"*.rpt",NULL, OFN_EXPLORER, szFilter);


        // Show the Open dialog box

        int iReturn = FileDlg.DoModal();

        

        if(iReturn == IDOK)

        {

            // Get the name and path of the report file selected from the

            // Open dialog box.

            _bstr_t FileName(FileDlg.GetPathName().AllocSysString());

            

            // Create the Application Object.

            m_Application.CreateInstance("CrystalDesignRuntime.Application");


            // Open the report selected from the Open dialog box.

            m_Report = m_Application->OpenReport(FileName);

        }

        else if (iReturn = IDCANCEL)

        {

            // Terminate the execution of the function if the

            // user cancels.

            return;

        }

    }

    

    // Set the report object to the Embeddable Designer.

    m_Designer.SetReportObject(m_Report);


    // Set the report souce for the Crystal Reports Viewer

    m_Viewer.SetReportSource(m_Report);

    

    // Set the width of the report page to the width of the viewing area

    m_Viewer.Zoom(1);

    

    // View the report in the Crystal Reports Viewer control.

    m_Viewer.ViewReport();


    // Check if the Save Report button is enabled.

    // If the button is disabled then enable the Save Report,

    // and Preview buttons. The Save Report button will always

    // be enabled once the first report is opened or created.

    BOOL bEnabled;

    bEnabled = m_DesignerSettings.IsWindowEnabled();


    if( bEnabled == FALSE)

    {

        m_SaveReport.EnableWindow(TRUE);

        m_Preview.EnableWindow(TRUE);

    }


    // Check if the Embeddable Designer is hidden.

    // If it is hidden then hide the Design button, show the

    // Preview button, hide the Crystal Reports Viewer, and

    // show the Embeddable Designer.

    if(!m_Designer.IsWindowVisible())

    {

        m_ShowDesigner.ShowWindow(SW_HIDE);

        m_Preview.ShowWindow(SW_SHOW);

        m_Viewer.ShowWindow(FALSE);

        m_Designer.ShowWindow(TRUE);

    }


}

  1. Add the code for the OnNewReport function.


// In this function call the InitReport function and pass a 
// value of True to create a new report. Passing a value of
// False opens an existing report.
void CEmbeddable_DesignerDlg::OnNewReport() 
{
    InitReport(TRUE);

}// In this function call the InitReport function and pass a

// value of True to create a new report. Passing a value of

// False opens an existing report.

void CEmbeddable_DesignerDlg::OnNewReport()

{

    InitReport(TRUE);

}


  1. Add the code for the OnOpenReport function.


// In this function call the InitReport function and pass 
// a value of False to open an existing report. Passing a 
// value of True creates a new report.
void CEmbeddable_DesignerDlg::OnOpenReport() 
{
    InitReport(FALSE);

}// In this function call the InitReport function and pass

// a value of False to open an existing report. Passing a

// value of True creates a new report.

void CEmbeddable_DesignerDlg::OnOpenReport()

{

    InitReport(FALSE);

}


  1. Add the code for the OnSaveReport function.

// In this function open a Save As dialog box and save the current 
// report to the selected path and name.
void CEmbeddable_DesignerDlg::OnSaveReport() 
{

    // Initialize the Save As dialog box to only display the Crystal
    // Report (.rpt) file type.
    CFileDialog FileDlg(FALSE,"*.rpt",NULL,OFN_EXPLORER, szFilter);
 
    // Show the Save As dialog box.
    int iReturn = FileDlg.DoModal();
        
    if(iReturn == IDOK)
    {
        // Get the path and name selected in the Save As dialog box. 
        _bstr_t FileName(FileDlg.GetPathName().AllocSysString());

        // Save the report to the path and name specified in the 
        // Save As dialog box.
        m_Designer.SaveReport(FileName);
    }
    else if(iReturn == IDCANCEL) 
    {
        // Terminate the execution of the function if the
        // user cancels.
        return;
    }

}// In this function open a Save As dialog box and save the current

// report to the selected path and name.

void CEmbeddable_DesignerDlg::OnSaveReport()

{


    // Initialize the Save As dialog box to only display the Crystal

    // Report (.rpt) file type.

    CFileDialog FileDlg(FALSE,"*.rpt",NULL,OFN_EXPLORER, szFilter);


    // Show the Save As dialog box.

    int iReturn = FileDlg.DoModal();

        

    if(iReturn == IDOK)

    {

        // Get the path and name selected in the Save As dialog box.

        _bstr_t FileName(FileDlg.GetPathName().AllocSysString());


        // Save the report to the path and name specified in the

        // Save As dialog box.

        m_Designer.SaveReport(FileName);

    }

    else if(iReturn == IDCANCEL)

    {

        // Terminate the execution of the function if the

        // user cancels.

        return;

    }

}


  1. Add the code for the OnPreview function

// In this function, refresh the report without refreshing the data 
// source, hide the Preview button and the Embeddable Designer, and 
// then show the Design button and the Crystal Report Viewer.
void CEmbeddable_DesignerDlg::OnPreview() 
{
    m_Viewer.RefreshEx(FALSE);
    m_Preview.ShowWindow(SW_HIDE);
    m_Designer.ShowWindow(SW_HIDE);
    m_ShowDesigner.ShowWindow(SW_SHOW);
    m_Viewer.ShowWindow(SW_SHOW);    

}// In this function, refresh the report without refreshing the data

// source, hide the Preview button and the Embeddable Designer, and

// then show the Design button and the Crystal Report Viewer.

void CEmbeddable_DesignerDlg::OnPreview()

{

    m_Viewer.RefreshEx(FALSE);

    m_Preview.ShowWindow(SW_HIDE);

    m_Designer.ShowWindow(SW_HIDE);

    m_ShowDesigner.ShowWindow(SW_SHOW);

    m_Viewer.ShowWindow(SW_SHOW);    

}


  1. Add the code for the OnShowDesigner function.

// In this function, hide the Design button and the Crystal Report 

// Viewer, and show the Preview button and the Embeddable Designer.

void CEmbeddable_DesignerDlg::OnShowDesigner()

{

    m_ShowDesigner.ShowWindow(SW_HIDE);

    m_Viewer.ShowWindow(SW_HIDE);    

    m_Preview.ShowWindow(SW_SHOW);

    m_Designer.ShowWindow(SW_SHOW);

}

// In this function, hide the Design button and the Crystal Report

// Viewer, and show the Preview button and the Embeddable Designer.

void CEmbeddable_DesignerDlg::OnShowDesigner()

{

    m_ShowDesigner.ShowWindow(SW_HIDE);

    m_Viewer.ShowWindow(SW_HIDE);    

    m_Preview.ShowWindow(SW_SHOW);

    m_Designer.ShowWindow(SW_SHOW);

}

To continue with this procedure, go to Step 4: Running the Embeddable Designer application (Microsoft Visual C++).



Seagate Software IMG Holdings, Inc.
http://www.seagatesoftware.com
Support services:
http://support.seagatesoftware.com